package com.qinglei.recoup.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.common.tenant.TenantContextHolder;
import com.qinglei.recoup.common.utils.RecoupUtil;
import com.qinglei.recoup.system.dao.RadarMapper;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.domain.DeanBed;
import com.qinglei.recoup.system.domain.Radar;
import com.qinglei.recoup.system.domain.RadarBed;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.DeanBedService;
import com.qinglei.recoup.system.service.RadarBedService;
import com.qinglei.recoup.third.domain.dto.RadarAddDto;
import com.qinglei.recoup.third.properties.FuXingProperties;
import com.qinglei.recoup.third.service.FuXingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 复星业务层实现
 *
 * @author yujunwei
 * @datetime 2022-05-04 21:22
 */
@Service
public class FuXingServiceImpl extends ServiceImpl<RadarMapper, Radar> implements FuXingService {

    @Autowired
    private BedService bedService;

    @Autowired
    private RadarBedService radarBedService;

    @Autowired
    private DeanBedService deanBedService;

    @Resource
    private FuXingProperties fuXingProperties;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(RadarAddDto radarAddDto) throws RecoupException {
        Integer tenantId = TenantContextHolder.getTenantId();
        TenantContextHolder.clear();
        Radar existsRadar = getOne(new QueryWrapper<Radar>().lambda().eq(Radar::getSnCode, radarAddDto.getSnCode()));
        if (null != existsRadar) {
            throw new RecoupException("该监测仪SN码已经存在");
        }
        TenantContextHolder.setTenantId(tenantId);
        Radar radar = new Radar();
        BeanUtils.copyProperties(radarAddDto, radar);
        boolean res = this.save(radar);
        if (!res) {
            throw new RecoupException("添加监测仪失败");
        }

        bindRadar(radar);

        return true;
    }

    private void bindRadar(Radar radar) throws RecoupException {
        Bed bed = new Bed();
        bed.setBuildingId(fuXingProperties.getBuildingId());
        bed.setFloorId(fuXingProperties.getFloorId());
        bed.setRoomId(fuXingProperties.getRoomId());
        bed.setName(radar.getSnCode());
        bed.setOutBedTimeout(fuXingProperties.getOutBedTimeout());
        if (!bedService.save(bed)) {
            throw new RecoupException("添加床位失败");
        }

        RadarBed radarBed = new RadarBed();
        radarBed.setBedId(bed.getId());
        radarBed.setRadarId(radar.getId());
        if (!radarBedService.save(radarBed)) {
            throw new RecoupException("监测仪床位绑定失败");
        }

        int type = BedService.TYPE_BED;
        int objId = bed.getId();
        Long userId = RecoupUtil.getCurrentUser().getId();
        DeanBed deanBed = new DeanBed();
        deanBed.setType(type);
        deanBed.setObjId(objId);
        deanBed.setUserId(userId);
        if (!deanBedService.save(deanBed)) {
            throw new RecoupException("用户床位绑定失败");
        }
    }

}
